$(document).ready(function(){
	// variables utilizadas
	var page = document.page.section.value;
	var items = $('.nb_item');
	$avatars = $('.fav_bg, .avatar_bg');
	// ---
	// Event handlers
	try{
		$('#hilite').css({left: $('span.nb_item, .active').position()['left']});
	}
	catch(e){
	}
	try{
		$('.nb_item').mouseover(function () {
			$('#hilite').stop().animate({left:$(this).position()['left']}, {duration:400});
		});
		$('span.nb_item').click(function () {
			$('span.nb_item').removeClass('active');
			$(this).addClass("active");
		});
		$('.nb_item').mouseout(function () {
			$('#hilite').stop().animate({left:$('span.nb_item.active').position()['left']}, {duration:400});
		});
	}
	catch(e){}
	//form event handlers
	//form validator

	//
	//--
	//start functions 
	
	$avatars.each(function(){
		draw_grid2(this);
	});
	
	//tag current page
	if( page == "Home" ){
		$(items[0]).addClass("active");
	}
	if( page == "Amigos"){
		$(items[1]).addClass("active");
	}
	if( page == "Mensajes"){
		$(items[2]).addClass("active");
	}
	if( page == "Preferencias"){
		$(items[3]).addClass("active");
	}
});

function check_fl(){
		var mail = $('#login_mail').val();
		var pass = $('#login_pass').val();
		var failed = false;
		if(mail.indexOf('.') == -1 || mail.indexOf('@') == -1){
			$('#login_mail_msg').text('Invalid e-mail address!');
			return false;
		}
		else{
			$('#login_mail_msg').text('');
		}
		if(pass.length < 3){
			$('#login_pass_msg').text('Check your password!');
			return false;
		}
		else{
			$('#login_mail_msg').text('');
		}
		return true;
}

function check_ff(){
		var name = $('#name').val();
		var lname = $('#lastname').val();
		var gender = $('#gender').val();
		var mail = $('#mail').val();
		var cmail = $('#mailcheck').val();
		var pass = $('#pass').val();
		var cpass = $('#passcheck').val();
		
		var failed = false;
		
		if( name.length < 4){
		//	$('#name_label').addClass('error');
			$('#name_msg').text('  Too short!');
			failed = true;
		}
		else{
		//	$('#name_label').removeClass('error');
			$('#name_msg').addClass('ok');
			$('#name_msg').text("Ok!");
		}
		if( lname.length < 4 ){
		//	$('#ln_label').addClass('error');
			$('#ln_msg').text('  Too short!');
			failed = true;
		}
		else{
		//	$('#ln_label').removeClass('error');
			$('#ln_msg').addClass('ok');
			$('#ln_msg').text("Ok!");
		}
		if( gender == '0' ){
			$('#gender_msg').text('Choose your sex!');
			failed = true;
		}
		else{
			$('#gender_msg').addClass('ok');
			$('#gender_msg').text("Ok!");
		}
		if(mail.indexOf('.') == -1 || mail.indexOf('@') == -1){
			$('#email_msg').text('Invalid e-mail address!');
			failed = true;
		}
		else if(mail != cmail){
			$('#email_msg').text('does not match!');
			$('#emailc_msg').text('does not match!');
			failed = true;
		}
		else{
			$('#email_msg').addClass('ok');
			$('#emailc_msg').addClass('ok');
			$('#email_msg').text('Ok!');
			$('#emailc_msg').text('Ok!');			
		}
		if(pass.length < 6){
			$('#pass_msg').text('6 chars at least!');
			failed = true;
		}
		else if(pass != cpass){
			$('#pass_msg').text('does not match!');
			$('#passc_msg').text('does not match!');
			failed = true;
		}
		else{
			$('#pass_msg').addClass('ok');
			$('#passc_msg').addClass('ok');
			$('#pass_msg').text('Ok!');
			$('#passc_msg').text('Ok!');
		}
		if( failed == true){
			return false;
		}
		else{
			return true;
		}
}
//

function send_msg(){
	friendm = document.sendmsg.friendmail.value;
	content = document.sendmsg.msg.value;
	csrf = document.sendmsg.csrfmiddlewaretoken.value;

	if(friendm.indexOf('.') == -1 || friendm.indexOf('@') == -1){
		alert('Mail inválido!');
		return false;
	}
	if(content.length < 1){
		alert('Ingrese algún mensaje');
		return false;
	}
	$('#alertbox').empty().append('Enviando mensaje...');
	$.post('sendmsg', {friendmail: friendm, msg: content, csrfmiddlewaretoken: csrf}, function(data){
		$('#alertbox').empty().append(data);
	});
	$('#sendmsg')[0].reset();
	return false;
}

function accept_request(){
	m_toaccept = document.acceptform.m_toaccept.value;
	csrf = document.acceptform.csrfmiddlewaretoken.value;
	t_id = '#'+m_toaccept;
	$('.rb_comment').each(function(){
		if($(this).attr('id') == m_toaccept){
			$(this).empty();
			div = $(this);
		}
	});
	div.empty().append('<table  width="600" style="border: 1px dotted orange; border-top: none;" ><tr><td align="center"><b>Agregando amigo...</b></td></tr>');
	$.post('/acceptrequest', {m_toaccept: m_toaccept, csrfmiddlewaretoken: csrf}, function(data){
		div.empty().append('<table  width="600" style="border: 1px dotted orange; border-top: none;" ><tr><td align="center"><b>'+data+'</b></td></tr>');
	}); 
	
	return false;
}

function add_friend(){
	friendm = document.addfriend.friendmail.value;
	if(friendm.indexOf('.') == -1 || friendm.indexOf('@') == -1){
		alert('Mail inválido!');
		return false;
	}
	csrf = document.addfriend.csrfmiddlewaretoken.value;
	$('#alertbox').empty().append('Enviando peticion...');
	$.post('/addfriend', {friendmail: friendm, csrfmiddlewaretoken: csrf},
	function(data){
		$('#alertbox').empty().append(data);
	});
	return false;
}

function sendstatus(){
	author = document.statf.author.value;
	status = document.statf.stat.value;
	csrf = document.statf.csrfmiddlewaretoken.value;
	document.statf.last_st.value = status;
	alert(author+status);
	$.post('/sendstatus', {author: author, status: status, csrfmiddlewaretoken: csrf}, function(data){
		$('#alertbox').empty().append(data);
	});
	window.location.reload();
	return false;
}

function send_comment(form){	
	to = form.s_id.value;
	content = form.c_content.value;
	if(content.length < 1){
		alert('Ingrese un comentario');
		return false;
	}
	csrf = form.csrfmiddlewaretoken.value;
	form.c_content.value= ''
	$.post('/comment', {s_id: to, c_content: content, csrfmiddlewaretoken: csrf}, function(data){
		$('#alertbox').append(data);
	});
	$('#'+to+'.comments').append('<b style="margin-left: 2em; color: orange">Recientemente usted dijo</b>: ' + content + '<br>');
	$('.CommentBox').each(function(){
		if($(this).attr('id') == to){
			$(this).slideUp('slow');
	}
	});
	return false;
}

function read_msg(id){
	t_id = "#" + id;
	$('.msg_c').each(function(){
		if($(this).attr('id') == id){
			$(this).slideDown('slow');
		}
	});
	$('.msg_s'+t_id).empty().append('<b style="color: green;">Leido</b>');
	$.get('/setasreaded', {msg_id: id} );
}

function msg_close(id){
//	alert(id);
	t_id = "#" + id;
	$('.msg_c').each(function(){
		if($(this).attr('id') == id){
			$(this).slideUp('slow');
		}
	});
}

function ShowCommentBox(obj){
	var sta = $(obj).attr("value");
	var box = $(obj).parents('.rb_comment').children('.CommentBox');
	if( sta == "Comentar"){
		box.show("slow");
		$(obj).attr("value","Ocultar");
	}
	else{
		box.hide("slow");
		$(obj).attr("value","Comentar");
	}
}

function checkForm(obj){
	alert($(obj).attr('id'));
}
//draw grid using jcanvas
function draw_grid2(obj){
	var w = $(obj).attr('width');
	var h = $(obj).attr('height');
	var size = 10;
	var lw = 0.3;
	for(var i = 0; i <= h; i += size){
		$(obj).drawLine({
			strokeStyle:"#444",
			strokeWidth: lw,
			x1: 0, y1: i,
			x2: w, y2: i,
		});
	}
	for(var i = 0; i <= w; i += size){
		$(obj).drawLine({
			strokeStyle:"#444",
			strokeWidth: lw,
			x1: i, y1: 0,
			x2: i, y2: h,
		});
	}
}
//draw grid using regular javascript
function draw_grid(obj){
	var w = $(obj).attr('width');
	var h = $(obj).attr('height');
	if($(obj)[0].getContext) {
		var ctx = $(obj)[0].getContext('2d');
		var size = 10;
		ctx.strokeStyle = "#444";
		ctx.beginPath();
		ctx.lineWidth = 0.3;
		for(var x=0; x <= w; x += size){
		ctx.moveTo(x,0)
		ctx.lineTo(x, h);
		}
		for(var y=0; y <= h; y += size){
			ctx.moveTo(0,y)
			ctx.lineTo(w, y);
		}
		ctx.stroke();
	}
	else{
		alert('No Context!');
	}
}

//not used. function to apply a border
function menu_select(obj)	{
	var items = $('.nb_item');
	for(var i = 0; i < items.length; i ++){
		if(items[i] != obj){
			items[i].style.border = '';
		}
	}
	obj.style.borderBottom = '1px solid white';
}

//not used.
function draw_LgBg(){
	var cnvs = document.getElementById('logo');
	if(cnvs.getContext) {
		var ctx = cnvs.getContext('2d');
		var lgrad = ctx.createRadialGradient(200,50,20,200,30,200);
	lgrad.addColorStop(0.1,  'rgba(255,255,255,0.4)');
	//	lgrad.addColorStop(0.15, 'rgba(115,181,197,0.1)');
		lgrad.addColorStop(0.8, 'rgba(115,181,197,0.1)');
		ctx.beginPath();
		ctx.lineWidth = 5;
		ctx.moveTo(200,50);
		for(var t = Math.PI; t <= 2*Math.PI; t += (Math.PI/12)){
			ctx.moveTo(200,50);
			ctx.lineTo(198+200*Math.cos(t),0);
			ctx.moveTo(200,50);
			ctx.lineTo(202+200*Math.cos(t),0);
			ctx.moveTo(200,50);
			ctx.lineTo(198+200*Math.cos(t),100)
			ctx.moveTo(200,50);
			ctx.lineTo(202+200*Math.cos(t),100)
		}
		for(var t = Math.PI; t <= 2*Math.PI; t += (Math.PI/5)){
			ctx.moveTo(200,50);
			ctx.lineTo(0,48+50*Math.cos(t));
			ctx.moveTo(200,50);
			ctx.lineTo(0,52+50*Math.cos(t));
			ctx.moveTo(200,50);
			ctx.lineTo(400,48+50*Math.cos(t));
			ctx.moveTo(200,50);
			ctx.lineTo(400,52+50*Math.cos(t));
		}
		ctx.strokeStyle = lgrad;
		ctx.stroke();
	}
}
